﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.service;
using DXApplicationTangche.DTO;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using DXApplicationTangche.UC.面料库存.入库.service;

namespace DXApplicationTangche.UC.订单一览新.辅料.辅料重构
{
    public partial class Form辅料SPU维护 : DevExpress.XtraEditors.XtraForm
    {
        private String subMaterialTypeId;
        private String subMaterialType;
        
        public Form辅料SPU维护()
        {
            InitializeComponent();
            this.searchLookUpEdit辅料类别.Properties.DataSource = 辅料Service.get辅料类别一览(false);
        }
        public Form辅料SPU维护(SubMaterialSpuDao subMaterialSpuDao)
        {
            InitializeComponent();
            this.textBox品番.ReadOnly = true;
            this.textBox品番.Text = subMaterialSpuDao.subMaterialCode;
            this.textBox辅料名称.Text = subMaterialSpuDao.subMaterialName;
            this.textBox供应商.Text = subMaterialSpuDao.subMaterialSupplier;
            this.searchLookUpEdit辅料类别.Text = subMaterialSpuDao.subMaterialType;
            this.subMaterialTypeId = subMaterialSpuDao.subMaterialTypeId;
            this.subMaterialType = subMaterialSpuDao.subMaterialType;
            this.searchLookUpEdit辅料类别.Properties.DataSource = 辅料Service.get辅料类别一览(false);
        }

        #region 选择辅料类别
        private void searchLookUpEdit辅料类别_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit辅料类别View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit辅料类别.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.subMaterialTypeId = this.searchLookUpEdit辅料类别.Properties.View.GetRowCellValue(rowHandle, "accessoryCatalogId").ToString();
                    this.subMaterialType = this.searchLookUpEdit辅料类别.Properties.View.GetRowCellValue(rowHandle, "accessoryCatalogName").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit辅料类别.ToolTip = null;
            searchLookUpEdit辅料类别.EditValue = null;
        }
        private void searchLookUpEdit辅料类别_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.subMaterialType;
        }
        #endregion

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            //判断辅料号输入是否合法
            MaterialMessageResult messageResult =
                辅料Service.辅料品番合法性判断(this.textBox品番.Text);
            if (messageResult.isLeagel)
            {
                try
                {
                    辅料Service
                        .saveSubMaterialSpu(
                            this.textBox品番.Text.Trim(),
                            this.textBox辅料名称.Text.Trim(),
                            this.textBox供应商.Text.Trim(),
                            this.subMaterialTypeId,
                            this.subMaterialType
                        );
                }
                catch (Exception)
                {
                    MessageBox.Show("操作失败");
                }
                this.Close();
            }
            else
            {
                MessageBox.Show(messageResult.errorMessage);
            }
        }
    }
}